package com.bdcom.autotest.interaction.http;

import java.net.Socket;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import com.bdcom.autotest.MainFrame;
import com.bdcom.autotest.ui.TopPanel;

public class ConsoleServerOper {
	
	private static Logger log = LoggerFactory.getLogger(ConsoleServerOper.class);
	
	public static void main(String[] args) {
		String csip = "192.168.40.139";
		
		

		
		int setBaudRate = setBaudRate(csip, baud9600);
		
		String baudRate = getBaudRate(csip);
		System.out.println("get current baud rate is : "+ baudRate    );
		
	}
	
	public static String baud115200 = "115200";
	public static String baud9600 = "9600";
	
	
	// 获取 串口波特率
	//	 User user = restTemplate.getForObject("http://localhost:8080/getUser/{param01}/{param02}", User.class, PARAM01, PARAM02);
	public static String getBaudRate(String csip) {
		
		String url = "http://" + csip + "/0201";
		
		RestTemplate restTemplate = gettemp();

		String htmls = restTemplate.getForObject( url , String.class );
		
//		System.out.println( htmls  );
		Document doc = Jsoup.parse(htmls);
		
		Elements select = doc.select("SELECT[name=2]");
		Element element = select.get(0);
		Elements options2 = element.getElementsByAttribute("selected");
		Element option = options2.get(0);
		String val = option.val();
//		System.out.println( val  );
		return val;
	}
	
	public static RestTemplate gettemp() {
		RestTemplate restTemplate = new RestTemplate();
//		RestTemplate restTemplate = CUtil.getRestTemp();
		return restTemplate;
	}
	
	public static int SETRES_SUCCESS = 1;
	public static int SETRES_SETFAIL = 2;
	public static int SETRES_REBOOTFAIL = 3;
	
	// 设置全部串口波特率
	// 重启console server 服务器
	public static int setBaudRate(String csip , String baud) {
		//http://192.168.40.139/0200?0=2&1=0&2=115200&3=8&4=1&5=2&6=0&7=0&8=50&9=1024&10=0&11=14&12=0&13=1000&14=0&E=%C8%B7%B6%A8
		//http://192.168.40.139/0200?0=2&1=0&2=115200&3=8&4=1&5=2&6=0&7=0&8=50&9=1024&10=0&11=14&12=0&13=1000&14=0&15=9&E=%C8%B7%B6%A8
		String url = "http://"+csip+"/0200?0=2&1=0&2="+baud+""
				+ "&3=8&4=1&5=2&6=0&7=0&8=50&9=1024&10=0&11=14&12=0&13=1000&14=0&15=9&E=%C8%B7%B6%A8";
		
		RestTemplate restTemplate = gettemp();
		String htmls = restTemplate.getForObject( url , String.class );
		
		Document doc = Jsoup.parse(htmls);
		
		Elements select = doc.select("SELECT[name=2]");
		Element element = select.get(0);
		Elements options2 = element.getElementsByAttribute("selected");
		Element option = options2.get(0);
		String val = option.val();
		
		
		log.info(  "设置完毕后，当前波特率:" + val  );
		
		if( val.equals(baud)  ) {
			
//			System.out.println("  Reboot...........");
			String rebootUrl = "http://"+csip+"/0800?E=%C8%B7%B6%A8";
			String htmls2 = restTemplate.getForObject( rebootUrl , String.class );
			Document doc2 = Jsoup.parse(htmls2);
			Elements fonts = doc2.select("font[color=003366]");
			Element font = fonts.get(0);
			String val2 = font.text();
			log.info(  val2  );
			
			boolean rebootFlag = false;
			if( val2.indexOf("设备正在重新启动")!=-1 ) {
				long currentTimeMillis = System.currentTimeMillis();
				try { Thread.sleep(1000); } catch (InterruptedException e) { }

				for(  int i=0 ;  i<100 ;i++) {
					boolean checkTCP80 = checkTCP80(csip);
					if( checkTCP80 ) {
						rebootFlag = true;
						break;
					}
					try { Thread.sleep(1000); } catch (InterruptedException e) { }
				}
				
				long currentTimeMillis2 = System.currentTimeMillis();
				long a = currentTimeMillis2-currentTimeMillis;
				log.info( "Reboot  coast time : " +  a + " ms."  );
			}
			if( !rebootFlag) {
				
				log.info(  "Console server 服务器重启失败."   );
				return SETRES_REBOOTFAIL;
			}
		}else {
			log.info(  "设置波特率失败......"  );
			return SETRES_SETFAIL;
		}
		
		if( MainFrame.getInstance()!=null ) {
			MainFrame.getInstance().getTopp().setBaudLabel(baud);
		}
		
		return SETRES_SUCCESS;
	}
	
	
	public static boolean checkTCP80(String ip ) {
		
		Socket socket = null;
		try { 
			socket = new Socket( ip , 80);
			socket.close();
			return true;
		} catch (Exception e)  {
		
		}finally {
			if( socket!=null ) {
				try {
					socket.close();
				} catch (Exception e) {
				}
			}
		}
		return false;
	}
	
	
	
}
